package com.woniuxy.yygh.hsop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.yygh.cmn.client.DictFeignClient;
import com.woniuxy.yygh.hsop.repository.HospitalRepository;
import com.woniuxy.yygh.hsop.service.HospitalService;
import com.woniuxy.yygh.model.hosp.Hospital;
import com.woniuxy.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 操作mongdb中的医院
 * <P> Date: 2022/3/1:18:09
 * <P> Description:
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    /**
     * TODO 操作mongdb
     */
    @Autowired
    private HospitalRepository hospitalRepository;

    /**
     * TODO 调用微服务接口
     */
    @Autowired
    private DictFeignClient dictFeignClient;


    /**
     * TODO: 医院上传接口 根据前端的json 保存数据到mongdb
     **/
    @Override
    public void save(Map<String, Object> parameterMap) {
        //map转为Hospital对象
        String mapString = JSONObject.toJSONString(parameterMap);

        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在相同数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在则更新
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //如果不存在 添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            // System.out.println(hospital.getLogoData());
            hospitalRepository.save(hospital);
        }
    }

    /**
     * TODO:根据医院编号查询医院
     **/
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * TODO:查询医院列表 并且分页
     *        实体可以根据省 市 区 和医院名字来查询
     **/
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        Pageable pageable = PageRequest.of(page - 1, limit, sort);

        //TODO 模糊查询 不区分大小写
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();
        //TODO 把vo的对象copy到普通的实体类里面
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        Example<Hospital> example = Example.of(hospital, matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //得到所有医院集合  遍历 对医院等级进行封装
        //封装到继承BaseMongoEntity 的 param里面
        //List<Hospital> content = pages.getContent();

        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return pages;
    }

    /**
     * TODO:更新医院上线状态 根据医院id更改status
     **/
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * TODO:根据医院id查询医院详情信息
     **/
    @Override
    public Map<String, Object> getHospById(String id) {

        Map<String, Object> result = new HashMap<>();

        Hospital hospital1 = hospitalRepository.findById(id).get();
       // System.out.println(hospital1);
        Hospital hospital =
                this.setHospitalHosType(hospital1);

        //分别封装
        result.put("hospital", hospital);

        //医院的一些规则 比如上下班时间和其他规则 预约信息
        result.put("bookingRule", hospital.getBookingRule());

        hospital.setBookingRule(null);
        return result;
    }

    /**
     * TODO: 根据医院编号 获取医院名称
     **/
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    /**
     * TODO:根据医院名称做模糊查询
     **/
    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * TODO:根据医院编号 获取医院预约挂号详情
     **/
    @Override
    public Map<String, Object> item(String hoscode) {

        Map<String, Object> result = new HashMap<>();

        //医院详情 根据医院编号获得医院  按照每个医院的等级进行封装
        Hospital hospital1 = this.setHospitalHosType(this.getByHoscode(hoscode));
       // System.out.println(hospital1);
        Hospital hospital =
                this.setHospitalHosType(hospital1);

        //分别封装
        result.put("hospital", hospital);

        //医院的一些规则 比如上下班时间和其他规则 预约信息
        result.put("bookingRule", hospital.getBookingRule());
        hospital.setBookingRule(null);

        return result;
    }

    /**
     * TODO: 获取查询list 遍历进行封装 按照每个医院的等级
     **/
    private Hospital setHospitalHosType(Hospital hospital) {
        //TODO 根据 diceCode和value获取医院等级名称 查询mongdb中的hopital表
        //TODO dict_code Hostype value = 1
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

        //TODO 查询省市区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        //TODO 封装地区 和 医院等级编号 几级医院
        hospital.getParam().put("hostypeString", hostypeString);
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);

        return hospital;
    }


}
